package org.dochandler.test.tester;

import static org.junit.Assert.assertEquals;

import org.dochandler.service.domain.Department;
import org.dochandler.service.domain.constants.Constants;
import org.dochandler.service.domain.mission.MissionRequest;
import org.dochandler.test.service.MissionApprovalServiceTest;
import org.dochandler.test.service.core.MissionApprovalServiceImplTest;
import org.esfinge.guardian.context.AuthorizationContext;
import org.esfinge.guardian.context.WrappedObj;
import org.esfinge.guardian.exception.AuthorizationException;
import org.junit.Before;
import org.junit.Test;

public class BudgetTester {
	private MissionRequest missionRequest;
	
	@Before
	public void setUp() {
		missionRequest = new MissionRequest();
	}
	
	/**
	 * Test of the Security Policy Id SP06
	 */
	@Test
	public void approveMissionWithinBudgetTest() {
		Department dp = new Department(1, "A Department");
		
		double REMAINING_BUDGET = 100d;
		double ESTIMATED_COST = 50d;
		
		dp.setRemainingBudget( REMAINING_BUDGET );
		missionRequest.setEstimatedCost( ESTIMATED_COST );
		
		WrappedObj<MissionRequest> w1 = AuthorizationContext.wrapAsResourceProp( Constants.MISSION_REQUEST.val(), missionRequest);
		WrappedObj<Department> w2 = AuthorizationContext.wrapAsEnvironmentProp(Constants.DEPARTMENT.val(), dp);
		
		MissionApprovalServiceTest mrs = AuthorizationContext.guardObject(new MissionApprovalServiceImplTest(), w1, w2);
		
		assertEquals("The result must be true", true, mrs.approveMissionForBudget(missionRequest) );
	}
	
	
	/**
	 * Test of the Security Policy Id SP06
	 */
	@Test(expected=AuthorizationException.class)
	public void approveMissionOffBudgetTest() {
		Department dp = new Department(1, "A Department");
		
		double REMAINING_BUDGET = 0d;
		double ESTIMATED_COST = 50d;
		
		dp.setRemainingBudget( REMAINING_BUDGET );
		missionRequest.setEstimatedCost( ESTIMATED_COST );
		
		WrappedObj<MissionRequest> w1 = AuthorizationContext.wrapAsResourceProp( Constants.MISSION_REQUEST.val(), missionRequest);
		WrappedObj<Department> w2 = AuthorizationContext.wrapAsEnvironmentProp(Constants.DEPARTMENT.val(), dp);
		
		MissionApprovalServiceTest mrs = AuthorizationContext.guardObject(new MissionApprovalServiceImplTest(), w1, w2);
		
		mrs.approveMissionForBudget(missionRequest);
	}


}
